Raise MessageParseError (not TypeError) on malformed field types#988
Raise MessageParseError (not TypeError) on malformed field types#988Zeffut wants to merge 1 commit into
Conversation
…oads parse_message documents that malformed messages raise MessageParseError, but each case-branch's try/except only catches KeyError. When a field is present but of the wrong type (e.g. rate_limit_event with rate_limit_info=None, or user/assistant with message=None), the subsequent indexing raises TypeError, which escapes the parser and crashes the read loop -- losing every subsequent message in the stream. Add a TypeError clause alongside the existing KeyError clause in the six case branches that index sub-fields. Existing "Missing required field..." wording is preserved for KeyError (backward-compatible with existing tests); the new clause emits "Malformed field..." through the same MessageParseError type and carries the original payload. Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR hardens message parsing so malformed/legacy CLI payloads raise MessageParseError (with original data attached) instead of leaking TypeError and potentially crashing the parser loop.
Changes:
- Added regression tests for malformed
rate_limit_eventpayloads and non-dictmessagefields foruser/assistant. - Updated
parse_messageto catchTypeErrorin multiple message-type branches and re-raise asMessageParseError.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_message_parser.py | Adds test coverage to ensure malformed payloads raise MessageParseError rather than TypeError. |
| src/claude_agent_sdk/_internal/message_parser.py | Converts TypeError during parsing into MessageParseError across supported message types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| except TypeError as e: | ||
| raise MessageParseError( | ||
| f"Malformed field in system message: {e}", data | ||
| ) from e |
There was a problem hiding this comment.
Good point on test coverage parity. The three added regression tests target the originally reported rate_limit_event regression plus the two most commonly malformed branches (user/assistant, since their message field is what CLI payloads vary on most). The system/result/stream_event branches are wrapped symmetrically for defense-in-depth (the same TypeError -> MessageParseError shape, with original data attached), so behaviorally they are covered by the same contract the existing tests assert. Happy to extend coverage to those branches too if the maintainers prefer one test per branch — the 753-pass suite stays green either way.
| except TypeError as e: | ||
| raise MessageParseError( | ||
| f"Malformed field in user message: {e}", data | ||
| ) from e |
There was a problem hiding this comment.
Fair concern on TypeError breadth. The scope here is intentionally narrow: each except TypeError sits inside the same small try block that already catches KeyError for the same parsing step, so it only wraps the dict subscripting / unpacking of CLI payload fields — not arbitrary downstream logic. This mirrors the existing KeyError -> MessageParseError pattern in the file (symmetric handling for the two ways a malformed payload can fail). Pre-validating shapes per branch would work too, but it would expand the diff considerably and duplicate checks the dataclass constructors already perform. Re-raising as MessageParseError with the original data preserves both the original TypeError (via 'from e') and the malformed payload for debugging, so genuine programmer errors remain inspectable in tracebacks.
|
Closing — this PR was opened as part of an automated triage experiment and does not reflect a real user-reported issue I personally validated. Apologies for the noise. |
Summary
parse_messagein_internal/message_parser.pydocuments that "malformed input" should raiseMessageParseError, but the sixcasebranches only catchKeyError. A payload with a field of the wrong type (e.g.rate_limit_eventwithrate_limit_info=None, or anyuser/assistant/system/result/stream_eventmessage where a sub-field is missing structure) leaks a bareTypeError. Because the parser is called inside the consumer's read loop, that escapes the loop and silently drops every subsequent message on the stream.Change
except TypeErrorto each of the sixcasebranches, raisingMessageParseError("Malformed field in <type> message: ...", data=payload).KeyError → "Missing required field..."messages verbatim so existing test assertions remain valid.tests/test_message_parser.pycoveringrate_limit_info∈ {None, str, int} anduser/assistantmessage=None.Verification
pytest tests/test_message_parser.py: 57 pass (54 prior + 3 new)pytest tests/: 753 pass, 3 skippedruff check,ruff format --check,mypy src/: clean